class Die
	attr_accessor :sides
	
	def initialize(sides)
		@sides = sides
	end
	
	def roll
		@sides[rand * @sides.length]
	end
end

class Tray
	attr_accessor :dice, :modifier
	
	def initialize(string = '')
		if string.match /(\d+)d([\d]+)/
			@dice = Array.new
			$1.to_i.times{|i| @dice << Die.new((1..$2.to_i).to_a)}
		end
		if string.match /([+-])\s*(\d+)/
			@modifier = $2.to_i
			@modifier *= -1 if $1 == '-'
		end
	end
	
	def roll
		rolls = @dice.collect{|die| die.send :roll}
		total = @modifier || 0
		rolls.each{|roll| total += roll}
		return {total: total, rolls: rolls}
	end
end

